﻿namespace Komunikator.Web
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using System.Web.Security;

    partial class CommunicatorDomainService
    {
        /// <summary>
        /// Get settings of user with specified id
        /// </summary>
        /// <param name="userId">user's id</param>
        /// <returns>user's settings</returns>
        public User_Settings GetUser_SettingsByUserName(string userName)
        {
            return ObjectContext.User_Settings.Where(u => u.aspnet_Users.UserName == userName).FirstOrDefault();
        }

        /// <summary>
        /// Get personal data of user with specified name
        /// </summary>
        /// <param name="userId">user's name</param>
        /// <returns>User's personal data</returns>
        public User_Personal_Data GetUser_Personal_DataByUserName(string userName)
        {
            return (from upd in ObjectContext.User_Personal_Data.Include("Countries").Include("Cities") where upd.aspnet_Users.UserName == userName select upd).FirstOrDefault();
        }

        public IQueryable<Cities> GetCitiesInCountry(string country)
        {
            return from c in ObjectContext.Cities where c.Countries.CountryName == country select c; 
        }

        public IQueryable<User_Personal_Data> SearchPeople(string firstName, string lastName, string sex, int? ageFrom, int? ageTo, string country, string city, string street)
        {
            string _sex;
            if (sex == "Mężczyzna")
                _sex = "M";
            else if (sex == "Kobieta")
                _sex = "F";
            else
                _sex = String.Empty;

            var result = 
                from upd in ObjectContext.User_Personal_Data.Include("Countries").Include("Cities").Include(@"aspnet_Users") 
                where 
                (upd.FirstName.Contains(firstName) || (firstName == String.Empty && upd.FirstName == null))  && 
                (upd.LastName.Contains(lastName) || (lastName == String.Empty && upd.LastName == null)) &&
                (_sex != String.Empty ? upd.Sex == _sex : true) && 
                (ageFrom.HasValue ? (upd.Age >= ageFrom) : true) &&
                (ageTo.HasValue ? (upd.Age <= ageTo) : true) &&
                (upd.Countries.CountryName.Contains(country) || (country == String.Empty && upd.Country == null)) &&
                (upd.Cities.CityName.Contains(city) || (city == String.Empty && upd.City == null)) &&
                (upd.Street.Contains(street) || (street == String.Empty && upd.Street == null))
                select upd;
            foreach (User_Personal_Data user in result)
            {
                Console.WriteLine(user.aspnet_Users.UserName);
            }
            return result;
        }

        [Invoke]
        public bool UserChangePassword(string userName, string oldPassword, string newPassword)
        {
            if (Membership.ValidateUser(userName, oldPassword))
            {
                MembershipUser memUser = Membership.GetUser(userName);
                return memUser.ChangePassword(oldPassword, newPassword);
            }
            return false;
        }

        public ContactLists GetContactListByUserName(string userName)
        {
            return (from cl in ObjectContext.ContactLists where cl.aspnet_Users.UserName == userName select cl).FirstOrDefault();
        }


    }
}